package cn.tedu.straw.gateway.service.impl;



import cn.tedu.straw.gateway.mapper.UserMapper;

import cn.tedu.straw.commons.model.Permission;
import cn.tedu.straw.commons.model.User;

import cn.tedu.straw.commons.security.UserInfo;
import cn.tedu.straw.gateway.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


import java.util.Arrays;
import java.util.List;


/**
 * @author shkstart
 * @create 2020-07-15 15:29
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper,User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RestTemplate restTemplate;


    //PermissionMapper permissionMapper;




    @Override
    public UserDetails login(String username) {
        //根据参数username查询用户信息

        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        User user = userMapper.selectOne(queryWrapper);
        //判断查询结果是否为null,有没有这个用户
        //注意:后续的验证和最终的界面是由Spring-Security显示的,此处不要抛出异常
        if (user == null) {
            return null;
        }
        //权限字符串数组
        // 以下url表示调用远程服务的API地址，其中，portal是在Eureka注册的服务名
        String url = "http://portal/v1/permissions/?userId={1}";
// 以下getForObject()方法的第2个参数表示调用其它服务后的“响应结果类型”
// 注意：如果响应结果类型是List，必须写成数组类型，否则会出错
        Permission[] permissionArray = restTemplate.getForObject(url,Permission[].class, user.getId());
        List<Permission> permissions = Arrays.asList(permissionArray);
        log.debug("远程调用获取权限列表 >>> {}", permissions);

        //List<Permission> permissions = permiss ionMapper.selectByUserId(user.getId());
        String[] authorities=new String[permissions.size()];
        for (int i = 0; i < permissions.size(); i++) {
            authorities[i]=permissions.get(i).getName();
        }
//        String[] authorities={
//                "test:user:info"
//        };
        //组织"用户详情"对象
        //TODO 未完
//        UserDetails userDetails= org.springframework.security.core.userdetails.User
//                .builder()
//                .username(user.getUsername())
//                .password(user.getPassword())
//                .authorities(authorities)
//                .disabled(user.getEnabled()==0)
//                .accountLocked(user.getLocked()==1)
//                .build();
        UserInfo userInfo = new UserInfo(user.getUsername(), user.getPassword(), user.getEnabled()==1, true, true, user.getLocked()==0, AuthorityUtils.createAuthorityList(authorities));
        userInfo.setId(user.getId());
        userInfo.setNickname(user.getNickname());
        userInfo.setGender(user.getGender());
        userInfo.setType(user.getType());
        return userInfo;
    }


}
